package com.cloud.cmt.usermanage.controller;

import com.angela.cloud.common.base.ResultMessage;
import com.angela.cloud.common.constant.ResultCodeConstant;
import com.cloud.cmt.usermanage.entity.Right;
import com.cloud.cmt.usermanage.entity.UserInfo;
import com.cloud.cmt.usermanage.po.TenantIdByUplinkPo;
import com.cloud.cmt.usermanage.service.RightRepository;
import com.cloud.cmt.usermanage.service.impl.RightServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: wcy
 * Date: 2017/10/17
 * Time: 16:14
 */
@RestController
@RequestMapping("/right")
@Api(value = "/权限接口", description = "权限增删查改等-by wcy")
public class RightCtl {

    @Resource
    RightRepository rightRepository;

    @Resource
    RightServiceImpl rightService;

    @ApiOperation(value = "通过id获取信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Right getUser(@PathVariable long id){
        return (Right) rightRepository.findOne(id);
    }

    @ApiOperation(value = "保存信息")
    @PostMapping(value = "")
    @ResponseBody
    public ResultMessage<Right> save(@RequestBody Right Right){
        ResultMessage resultMessage = new ResultMessage();
        Right = rightRepository.save(Right);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setId(Right.getId()+"");

        return resultMessage;
    }

    @ApiOperation(value = "更新信息")
    @PutMapping(value = "")
    @ResponseBody
    public ResultMessage<Right> update(@RequestBody Right Right){
        ResultMessage resultMessage = new ResultMessage();
        rightRepository.saveAndFlush(Right);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);

        return resultMessage;
    }


    @ApiOperation(value = "权限查询")
    @GetMapping(value = "/getRightTree")
    @ResponseBody
    public ResultMessage<List<Right>> getRightTree(){
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setData(rightService.getRightTree(-1l));
        return resultMessage;
    }

    @ApiOperation(value = "通过id删除")
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") Long id) {
        ResultMessage resultMessage = new ResultMessage();
        rightRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return  resultMessage;
    }

}
